﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Slja.DBUtility;//Please add references
using Slja.Model;
namespace Slja.DAL
{
    /// <summary>
    /// 数据访问类:biz_EnumBasicData
    /// </summary>
    public partial class EnumBasicDataDAL
    {
        public EnumBasicDataDAL()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string Class, string Code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from biz_EnumBasicData");
            strSql.Append(" where Class=@Class and Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Class", SqlDbType.NVarChar,20),
					new SqlParameter("@Code", SqlDbType.NVarChar,20)			};
            parameters[0].Value = Class;
            parameters[1].Value = Code;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(EnumBasicDataModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into biz_EnumBasicData(");
            strSql.Append("Class,Code,Text,Memo)");
            strSql.Append(" values (");
            strSql.Append("@Class,@Code,@Text,@Memo)");
            SqlParameter[] parameters = {
					new SqlParameter("@Class", SqlDbType.NVarChar,20),
					new SqlParameter("@Code", SqlDbType.NVarChar,20),
					new SqlParameter("@Text", SqlDbType.NVarChar,50),
					new SqlParameter("@Memo", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.Class;
            parameters[1].Value = model.Code;
            parameters[2].Value = model.Text;
            parameters[3].Value = model.Memo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(EnumBasicDataModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update biz_EnumBasicData set ");
            strSql.Append("Text=@Text,");
            strSql.Append("Memo=@Memo");
            strSql.Append(" where Class=@Class and Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Text", SqlDbType.NVarChar,50),
					new SqlParameter("@Memo", SqlDbType.NVarChar,100),
					new SqlParameter("@Class", SqlDbType.NVarChar,20),
					new SqlParameter("@Code", SqlDbType.NVarChar,20)};
            parameters[0].Value = model.Text;
            parameters[1].Value = model.Memo;
            parameters[2].Value = model.Class;
            parameters[3].Value = model.Code;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string Class, string Code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from biz_EnumBasicData ");
            strSql.Append(" where Class=@Class and Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Class", SqlDbType.NVarChar,20),
					new SqlParameter("@Code", SqlDbType.NVarChar,20)			};
            parameters[0].Value = Class;
            parameters[1].Value = Code;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public EnumBasicDataModel GetModel(string Class, string Code)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Class,Code,Text,Memo from biz_EnumBasicData ");
            strSql.Append(" where Class=@Class and Code=@Code ");
            SqlParameter[] parameters = {
					new SqlParameter("@Class", SqlDbType.NVarChar,20),
					new SqlParameter("@Code", SqlDbType.NVarChar,20)			};
            parameters[0].Value = Class;
            parameters[1].Value = Code;

            EnumBasicDataModel model = new EnumBasicDataModel();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                AssignModel(ds.Tables[0].Rows[0], model);
                return model;
            }
            else
            {
                return null;
            }
        }
        public void AssignModel(DataRow dr, Model.EnumBasicDataModel model)
        {
            if (dr["Class"] != null && dr["Class"].ToString() != "")
            {
                model.Class = dr["Class"].ToString();
            }
            if (dr["Code"] != null && dr["Code"].ToString() != "")
            {
                model.Code = dr["Code"].ToString();
            }
            if (dr["Text"] != null && dr["Text"].ToString() != "")
            {
                model.Text = dr["Text"].ToString();
            }
            if (dr["Memo"] != null && dr["Memo"].ToString() != "")
            {
                model.Memo = dr["Memo"].ToString();
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Class,Code,Text,Memo ");
            strSql.Append(" FROM biz_EnumBasicData ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }      

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM biz_EnumBasicData ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
       
        #endregion  Method
    }
}

